package com.mc.book.controller;

import com.mc.book.enums.ResponseEnum;
import com.mc.book.pojo.Result;
import com.mc.book.pojo.request.RequestUser;
import com.mc.book.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Api(tags = "用户模块")
@RestController
public class UserApi extends BaseApi{
	private static final Logger log = LoggerFactory.getLogger(UserApi.class);
	@Autowired
	private UserService userService;

	@ApiOperation("用户注册")
	@PostMapping("mc/user/reg")
	public Result userReg(@RequestBody RequestUser request){
		try {
			if(!request.regCheckFields()) return Result.fail(ResponseEnum.REQUEST_ENTRY_MISSING.getCode(),ResponseEnum.REQUEST_ENTRY_MISSING.getMessage());
			return userService.userReg(request);
		}catch (Exception e){
			log.error("系统异常：", e);
			return Result.fail(ResponseEnum.UP_SYSTEM_FAILURE.getCode(),ResponseEnum.UP_SYSTEM_FAILURE.getMessage());
		}
	}

	@ApiOperation("用户登录")
	@PostMapping("mc/user/login")
	public Result userLogin(@RequestBody RequestUser request){
		try {
			if(!request.loginCheckFields()) return Result.fail(ResponseEnum.REQUEST_ENTRY_MISSING.getCode(),ResponseEnum.REQUEST_ENTRY_MISSING.getMessage());
			return userService.userLogin(request);
		}catch (Exception e){
			log.error("系统异常：", e);
			return Result.fail(ResponseEnum.UP_SYSTEM_FAILURE.getCode(),ResponseEnum.UP_SYSTEM_FAILURE.getMessage());
		}
	}

	@ApiOperation("获取用户状态")
	@PostMapping("mc/user/info")
	public Result userInfo(@RequestBody RequestUser request){
		try {
			if(!request.infoCheckFields()) return Result.fail(ResponseEnum.REQUEST_ENTRY_MISSING.getCode(),ResponseEnum.REQUEST_ENTRY_MISSING.getMessage());
			String user_phone = redis.getObject(request.getToken(), String.class);
			user_phone = user_phone.replace("userToken:","");
			RequestUser info = userService.getInfo(user_phone);
			info.setUser_pass("");
			return Result.success(info);
		}catch (Exception e){
			log.error("系统异常：", e);
			return Result.fail(ResponseEnum.UP_SYSTEM_FAILURE.getCode(),ResponseEnum.UP_SYSTEM_FAILURE.getMessage());
		}
	}



	@ApiOperation("验证码")
	@PostMapping("mc/user/validateCode")
	public Result validateCode(){
		try {
			return userService.validateCode();
		}catch (Exception e){
			log.error("系统异常：", e);
			return Result.fail(ResponseEnum.UP_SYSTEM_FAILURE.getCode(),ResponseEnum.UP_SYSTEM_FAILURE.getMessage());
		}
	}


}
